﻿Imports ajayumi.develop.win32.GRssReader.Service
Imports System.IO
Imports System.ServiceModel.Syndication
Imports System.Xml
Imports ajayumi.develop.win32.GRssReader.Service.Model
Imports ajayumi.develop.VBasic.Core

Public Class ReaderService
    Implements IReaderService

    Private m_CacheFolder As String = String.Empty
    Private m_Feed As SyndicationFeed = Nothing

    Sub New(cacheFolder As String)
        ' TODO: Complete member initialization 
        m_CacheFolder = cacheFolder
    End Sub



    Public Sub AddState(item As Service.Model.ArticleInfo, state As Service.Model.State) Implements Service.IReaderService.AddState

    End Sub

    Public Sub AddSubscription(feed As String) Implements Service.IReaderService.AddSubscription

    End Sub

    Public Sub AddTags(item As Service.Model.ArticleInfo, ParamArray tags() As String) Implements Service.IReaderService.AddTags

    End Sub

    Public Property CacheFolder As String Implements Service.IReaderService.CacheFolder
        Get
            Return Me.m_CacheFolder
        End Get
        Set(value As String)
            Me.m_CacheFolder = value
        End Set
    End Property

    Public Property Feed As System.ServiceModel.Syndication.SyndicationFeed Implements Service.IReaderService.Feed
        Get
            Return m_Feed
        End Get
        Set(value As System.ServiceModel.Syndication.SyndicationFeed)
            Me.m_Feed = value
        End Set
    End Property

    Public Function GetArticleInfos(subscriptionInfo As SubscriptionInfo, limit As Integer) As System.Collections.Generic.IEnumerable(Of Service.Model.ArticleInfo) Implements Service.IReaderService.GetArticleInfos
        Dim _Directory As String = Path.Combine(Me.m_CacheFolder, StringHelper.FilterInvalidChars(subscriptionInfo.Title.Text))
        Dim _Path As String = Path.Combine(_Directory, "ArticleInfos.xml")
        If Directory.Exists(_Directory) = False OrElse File.Exists(_Path) = False Then
            Return Nothing
        End If

        Using _StreamReader As New StreamReader(_Path)
            Using _XmlReader As XmlReader = XmlReader.Create(_StreamReader, New XmlReaderSettings())
                Me.Feed = SyndicationFeed.Load(_XmlReader)
            End Using
        End Using

        Return Me.Feed.Items.Select(Function(o) New ArticleInfo(o))
    End Function


    Public Function GetStateItems(state As Service.Model.State, limit As Integer) As System.Collections.Generic.IEnumerable(Of Service.Model.ArticleInfo) Implements Service.IReaderService.GetStateItems
        Return Nothing
    End Function

    Public Function GetSubscriptionInfoList() As System.Collections.Generic.List(Of Service.Model.SubscriptionInfo) Implements Service.IReaderService.GetSubscriptionInfoList
        Dim _Path As String = Path.Combine(Me.m_CacheFolder, Service.Constants.SubscriptionFileName)
        Dim _SubscriptionInfoList As List(Of Model.SubscriptionInfo) = Nothing
        Dim _XElement As XElement = XElement.Load(_Path)
        _SubscriptionInfoList = _XElement.Elements("list").Elements("object").Select(Function(o) New SubscriptionInfo(o)).ToList()

        Return _SubscriptionInfoList
    End Function

    Public Function GetTagItems(tag As String, limit As Integer) As System.Collections.Generic.IEnumerable(Of Service.Model.ArticleInfo) Implements Service.IReaderService.GetTagItems
        Return Nothing
    End Function

    Public Property IsLogin As Boolean Implements Service.IReaderService.IsLogin
        Get
            Return False
        End Get
        Set(value As Boolean)

        End Set
    End Property

    Public Sub RemoveState(item As Service.Model.ArticleInfo, state As Service.Model.State) Implements Service.IReaderService.RemoveState

    End Sub

    Public Sub RemoveTag(item As Service.Model.ArticleInfo, tag As String) Implements Service.IReaderService.RemoveTag

    End Sub

    Public Sub RemoveTag(tag As String) Implements Service.IReaderService.RemoveTag

    End Sub

    Public Sub RenameTag(tag As String, newName As String) Implements Service.IReaderService.RenameTag

    End Sub

    Public Sub TagSubscription(feed As String, folder As String) Implements Service.IReaderService.TagSubscription

    End Sub

    Public Sub Update() Implements Service.IReaderService.Update

    End Sub

#Region "IDisposable Support"
    Private disposedValue As Boolean ' 检测冗余的调用

    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 释放托管状态(托管对象)。
            End If

            ' TODO: 释放非托管资源(非托管对象)并重写下面的 Finalize()。
            ' TODO: 将大型字段设置为 null。
        End If
        Me.disposedValue = True
    End Sub

    ' TODO: 仅当上面的 Dispose(ByVal disposing As Boolean)具有释放非托管资源的代码时重写 Finalize()。
    'Protected Overrides Sub Finalize()
    '    ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean)中。
    '    Dispose(False)
    '    MyBase.Finalize()
    'End Sub

    ' Visual Basic 添加此代码是为了正确实现可处置模式。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean)中。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
